package com.trans.handler;

import com.trans.anno.RemoteRequest;
import com.trans.anno.Translator;
import com.trans.auxiliary.HandleFullRecord;
import com.trans.auxiliary.HandleRecord;
import com.trans.auxiliary.Params;
import com.trans.entity.RequestInfo;
import com.trans.utils.HttpRemoteUtils;
import com.trans.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.hc.core5.http.HttpRequestInterceptor;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import java.text.MessageFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description mybatisPlus转换对象，目前支持唯一键字段
 * @Author xwduo
 * @Date 2022年03月01日 08:51
 **/
@Slf4j
@SuppressWarnings("all")
public class DbUnique implements TranslateHandle<Map<String,Object>> {

    private static final String SPLITER = "::";
    private static final String COLUM = "`%s`";
    private static final String COMMA = ",";

    @Override
    public Object doHandle(HandleRecord record,  Map<String, Map<String,Object>> cacheMap) {
        RemoteRequest dataBase = record.getAnnotation(RemoteRequest.class);
        Object curItemVal = record.getVal();
        Map<String,Object> showMap = new HashMap<>();
        if (null != dataBase && dataBase.showColum() !=null && dataBase.showColum().length>0 && null != curItemVal) {
            String table = dataBase.table();
            String colum = dataBase.colum();
            String columVal = String.valueOf(curItemVal);
            String uniqueKey = convert2Key(table, colum, columVal);
            Map<String, Object> objectMap = cacheMap.get(uniqueKey);
            for (String showColum : dataBase.showColum()) {
                showMap.put(showColum,objectMap.get(showColum));
            }
        }
        return showMap;
    }

    @Override
    public  Map<String, Map<String,Object>> cache(List<HandleFullRecord> recordList) {
        //
        Map<String, Set<String>> searchMap = new HashMap<>();
        Map<String, Set<String>> keyMaps = new HashMap<>();
        for (HandleFullRecord handleFullRecord : recordList) {
            Translator translator = handleFullRecord.translator;
            Params params = handleFullRecord.params;
            Object val = handleFullRecord.getVal();
            RemoteRequest remoteRequest = handleFullRecord.getAnnotation(RemoteRequest.class);
            //
            RequestInfo info = new RequestInfo(remoteRequest);

        }
        Map<String, Map<String,Object>> dataMap = new HashMap<>();
        HttpRemoteUtils.request()
        return dataMap;
    }

    @Override
    public int order() {
        return 0;
    }

    private static String convert2Key(String ... keys){
        return Arrays.stream(keys).collect(Collectors.joining(SPLITER));
    }

    private static String[] convert2Info(String key){
        return key.split(SPLITER);
    }

    private static String convert2SafeSearch(String key){
        return String.format(COLUM,key);
    }
}
